<?php
namespace addons\mall\controller;

use addons\mall\model\Spec;
use app\admin\controller\Addon;
use think\facade\Request;

/**
 * Class Back
 * @package app\admin\controller\Addon
 * NotParse
 */
class Goods extends Addon
{
    public function __construct()
    {
        parent::__construct();
        // 规格魔板
        $specs = (new Spec())->where('status', 1)->select()->toArray();
        $this->assign('specs', $specs);
    }

    /**
     * 基类文件use app\admin\controller\Addon;
     */

    /**
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count                      = $this->model
                ->withJoin('cate', 'LEFT')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin('cate', 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     */
    public function stock()
    {
        $id  = Request::param('id');
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['total_stock'] = $row->total_stock + $post['stock'];
                $post['stock']       = $row->stock + $post['stock'];
                $save                = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post             = $this->request->post();
            $authIds          = $this->request->post('auth_ids', []);
            $post['auth_ids'] = implode(',', array_keys($authIds));
            $rule             = [];
            $this->validate($post, $rule);
        
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            // dd($post);
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        return $this->fetch();
    }

    public function edit()
    {
        $id  = Request::param('id');
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
       
        $this->assign('row', $row);
        return $this->fetch();
    }

}
